﻿#include "plcdataconversion.h"

namespace PLCDataConversion {
//转换：0-300 → 0-24576
int16_t convert_300to24576(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)300);
    return static_cast<int16_t>(limited * 81.92); // 300*81.92=24576
}

//转换：转换：0-1000 → 0-2000
int16_t convert_1000to24576(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)1000);
    return static_cast<int16_t>(limited * 24.576); // 1000 * 24.576 = 24576
}

//转换：0-3000 → 0-24576
int16_t convert_3000to24576(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)3000);
    return static_cast<int16_t>(limited * 8.192); // 3000*8.192=24576
}

//转换：0-2666 → 0-20000
int16_t convert_2666to20000(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)2666);
    return static_cast<int16_t>(limited * (20000.0 / 2666)); // 2666 * (20000/2666) ≈ 20000
}

//转换：0-120 → 0-4000
int16_t convert_120to4000(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)120);
    return static_cast<int16_t>(limited * (4000.0 / 120)); // 120 * (4000/120) = 4000
}

//转换：0-100 → 0-1000
int16_t convert_100to1000(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)100);
    return static_cast<int16_t>(limited * 10.0); // 100 * 10 = 1000
}
// 转换：0-10 → 0-24576
int16_t convert_10to24576(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)10);
    return static_cast<int16_t>(limited * 2457.6);  // 10 * 2457.6 = 24576
}

// 转换：0-100 → 0-24576
int16_t convert_100to24576(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)100);
    return static_cast<int16_t>(limited * 245.76);  // 100 * 245.76 = 24576
}

// 转换：0-30 → 0-24576
int16_t convert_30to24576(int16_t value)
{
    int16_t limited = qBound((int16_t)0, value, (int16_t)30);
    return static_cast<int16_t>(limited * 819.2); // 30*819.2=24576
}



//转换：0-24576 → 0-300
int16_t convert_24576to300(int16_t value)
{
    int limited = value > 24576 ? 24576 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / 81.92); // 24576 / 81.92 = 300
}

//转换：0-24576 → 0-1000
int16_t convert_24576to1000(int16_t value)
{
    int limited = value > 24576 ? 24576 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / 24.576); // 24576 / 24.576 = 1000
}

//转换：0-24576 → 0-3000
int16_t convert_24576to3000(int16_t value)
{
    int limited = value > 24576 ? 24576 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / 8.192); // 24576 / 8.192 = 3000
}
//转换：0-20000 → 0-2666
int16_t convert_20000to2666(int16_t value)
{
    int limited = value > 20000 ? 20000 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / (20000.0 / 2666)); // 20000 / (20000/2666) = 2666
}

//转换：0-4000 → 0-120
int16_t convert_4000to120(int16_t value)
{
    int limited = value > 4000 ? 4000 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / (4000.0 / 120)); // 4000 / (4000/120) = 120
}

//转换：0-1000 → 0-100
int16_t convert_1000to100(int16_t value)
{
    int limited = value > 1000 ? 1000 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / 10.0); // 1000 / 10 = 100
}
// 反向转换：0-24576 → 0-10
int16_t convert_24576to10(int16_t value)
{
    int limited = value > 24576 ? 24576 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / 2457.6);  // 24576 / 2457.6 = 10
}

// 反向转换：0-24576 → 0-100
int16_t convert_24576to100(int16_t value)
{
    int limited = value > 24576 ? 24576 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / 245.76);  // 24576 / 245.76 = 100
}

// 反向转换：0-24576 → 0-30
int16_t convert_24576to30(int16_t value)
{
    int limited = value > 24576 ? 24576 : value;
    limited = limited < 0 ? 0 : limited;
    return static_cast<int16_t>(limited / 819.2); // 24576 / 819.2 = 30
}

}



